/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.flowable.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.RemoteGroup;
import org.flowable.ui.common.model.RemoteUser;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.flowable.ui.common.model.UserRepresentation;
import org.flowable.ui.common.security.DefaultPrivileges;
import org.flowable.ui.common.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moon.admin.framework.util.ToolUtils;
import com.moon.admin.sys.entity.SysRoleEntity;
import com.moon.admin.sys.entity.SysUserEntity;
import com.moon.admin.sys.entity.SysUserRoleEntity;
import com.moon.admin.sys.service.SysRoleService;
import com.moon.admin.sys.service.SysUserRoleService;
import com.moon.admin.sys.service.SysUserService;

/**
 * 流程设计器 登录，选择用户、角色
 * @author ninglong
 */
@RestController
@RequestMapping("/flowable/rest")
public class FlowableAccountController {

	@Autowired
	private SysUserService userService;
	@Autowired
	private SysRoleService roleService;
	@Autowired
	private SysUserRoleService userRoleService;
	
	/**
	 * flowable的账户 认证
	 */
	@GetMapping(value="account")
	public UserRepresentation getAccount() {
		SysUserEntity userEntity = userService.getById(1);//TODO 写死超级管理员登录，暂时不知会引起什么问题
		UserRepresentation userRepresentation = this.getUserRepresentation(userEntity);
		return userRepresentation;
	}

	/**
	 * 获取用户
	 * @param filter
	 * @return
	 */
	@GetMapping(value = "editor-users")
	public ResultListDataRepresentation getUsers(@RequestParam(value = "filter", required = false) String filter) {
		QueryWrapper<SysUserEntity> qw = new QueryWrapper<SysUserEntity>().eq("status", 0);
		if(ToolUtils.isNotEmpty(filter)){
			qw.like("userName", filter);
		}
		List<SysUserEntity> list = userService.list(qw);
		List<UserRepresentation> userRepresentations = new ArrayList<UserRepresentation>();
		if(list==null || list.size()==0){
			return new ResultListDataRepresentation(userRepresentations);
		}
		for(int i = 0; i < list.size(); i++){
			UserRepresentation userRepresentation = this.getUserRepresentation(list.get(i));
			userRepresentations.add(userRepresentation);
		}
		return new ResultListDataRepresentation(userRepresentations);
	}

	/**
	 * 获取用户组-相当于系统角色
	 * @param filter
	 * @return
	 */
	@GetMapping(value = "editor-groups")
	public ResultListDataRepresentation getGroups(@RequestParam(value = "filter",required = false) String filter) {
		LambdaQueryWrapper<SysRoleEntity> qw = Wrappers.<SysRoleEntity>lambdaQuery();
		if(ToolUtils.isNotEmpty(filter)){
			qw.like(SysRoleEntity::getRoleName, filter);
		}
		List<SysRoleEntity> list = roleService.list(qw);
		List<GroupRepresentation> groupRepresentations = new ArrayList<GroupRepresentation>();
		if(list==null || list.size()==0){
			return new ResultListDataRepresentation(groupRepresentations);
		}
		for(int i = 0; i < list.size(); i++){
			GroupRepresentation groupRepresentation = this.getGroupRepresentation(list.get(i));
			groupRepresentations.add(groupRepresentation);
		}
		return new ResultListDataRepresentation(groupRepresentations);
	}

	/**
	 * 系统用户转换成功flowable的用户
	 * @param userEntity
	 * @return
	 */
	private UserRepresentation getUserRepresentation(SysUserEntity userEntity){
		if(userEntity == null){
			return null;
		}
		//设置用户即sysUser->user
		org.flowable.idm.api.User idmUser = new RemoteUser();
		idmUser.setId(String.valueOf(userEntity.getId()));
		idmUser.setFirstName(userEntity.getUserName());
		SecurityUtils.assumeUser(idmUser);
		UserRepresentation userRepresentation = new UserRepresentation(idmUser);

		List<String> privileges = new ArrayList<String>();
		privileges.add(DefaultPrivileges.ACCESS_IDM);
		privileges.add(DefaultPrivileges.ACCESS_MODELER);
		privileges.add(DefaultPrivileges.ACCESS_TASK);
		privileges.add(DefaultPrivileges.ACCESS_ADMIN);
		privileges.add(DefaultPrivileges.ACCESS_REST_API);
		userRepresentation.setPrivileges(privileges);

		List<GroupRepresentation> groups = new ArrayList<GroupRepresentation>();
		List<Integer> roleIdList = userRoleService.list(Wrappers.<SysUserRoleEntity>lambdaQuery()
				.eq(SysUserRoleEntity::getUserId, userEntity.getId())).stream().map(entity->entity.getRoleId()).collect(Collectors.toList());
		if(ToolUtils.isEmpty(roleIdList)) {
			return userRepresentation;
		}
		List<SysRoleEntity> roleEntities = roleService.list(Wrappers.<SysRoleEntity>lambdaQuery().in(SysRoleEntity::getId, roleIdList));
		for(int i = 0; i < roleEntities.size(); i++){
			GroupRepresentation groupRepresentation = this.getGroupRepresentation(roleEntities.get(i));
			groups.add(groupRepresentation);
		}
		userRepresentation.setGroups(groups);
		return userRepresentation;
	}

	/**
	 * 系统角色转换成flowable的group组
	 * @param sysRole
	 * @return
	 */
	private GroupRepresentation getGroupRepresentation(SysRoleEntity roleEntity){
		if(roleEntity==null){
			return null;
		}
		//设置用户组即HrPost->group
		RemoteGroup group = new RemoteGroup();
		group.setId(String.valueOf(roleEntity.getId()));
		group.setName(roleEntity.getRoleName());
//		group.setType(sysRole.getRoleKey());
		return new GroupRepresentation(group);
	}
}
